// pages/orderinfor/orderinfor.js
const request = require('@/utils/request.js')
const constants = require('@/utils/khyConstants.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userType: '3',
    orderInfo: {},
    payModeList: getApp().globalData.dictData['payMode'],
    insureStatusList: getApp().globalData.dictData['insureStatus'],
    goodsTypeList: getApp().globalData.dictData['goodsType'],
    outerPackTypeList: getApp().globalData.dictData['outerPackType'],
    payModeName: '',
    goodsTypeName: '',
    insureStatusName: '',
    outerPackTypeName: '',
    dataList: [],
    stepList: [
      {label:'已下单', content: '暂无', time: ''},
      {label:'已揽收', content: '暂无', time: ''},
      {label:'运输中', content: '暂无', time: ''},
      {label:'已送达', content: '暂无', time: ''},
      {label:'已签收', content: '暂无', time: ''},
    ],
    currentStep: 0,
    imgList:[],// 上传的图片
    signImgList: [],//签收照片
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getPageData()
  },

  getPageData() {
    let order = getApp().globalData.pageData[constants.page_orderinfo]
    if (order && order.keyId) {
      let userInfo = getApp().getUserInfo()
      request.get('/UserController/getOrderLogisticsById', {keyId: order.keyId}).then(res => {
        if (res.data.code == constants.resultSuccessCode &&
          res.data.result) {
            let data = res.data.result || []
            let newStepList = this.data.stepList
            for (let i = 0, n = newStepList.length; i < n; i++) {
              if (i < data.length) {
                newStepList[i]['content'] = data[i]['logisticsDesc']
                newStepList[i]['time'] = data[i]['logisticsTime']
              } else {
                newStepList[i]['content'] = '暂无'
                newStepList[i]['time'] = ''
              }
            }
            let obj = {}
            obj.userType = userInfo.userType || '3'
            for (const key in order) {
              if (order.hasOwnProperty(key)) {
                obj['orderInfo.' + key] = order[key] || ''
              }
            }
            if (order['payMode']) {
              obj.payModeName = this.data.payModeList[order['payMode'] -1]['label']
            }
            if (order['goodsType']) {
              this.data.goodsTypeList.forEach(item => {
                if (order['goodsType'] == item['value']) {
                  obj.goodsTypeName = item['label']
                }
              })
            }
            if (order['insureStatus']) {
              this.data.insureStatusList.forEach(item => {
                if (order['insureStatus'] == item['value']) {
                  obj.insureStatusName = item['label']
                }
              })
            }
            if (order['outerPackType']) {
              this.data.outerPackTypeList.forEach(item => {
                if (order['outerPackType'] == item['value']) {
                  obj.outerPackTypeName = item['label']
                }
              })
              
            }
            obj.stepList = newStepList
            obj.currentStep = data.length == 0 ? 0 : data.length - 1
            if (order.goodsPhoto) {
              let goodsPhoto = order.goodsPhoto.split(",")
              let imgList=[];
              if(goodsPhoto.length>0){
                goodsPhoto.forEach((item)=>{
                    if(item!= ""){
                        imgList.push(getApp().globalData.nginxIp+"/"+item)
                    }
                })
                obj['imgList']=imgList
              }
            }

            if (order.signPhoto) {
              let signPhoto = order.signPhoto.split(",")
              let signImgList=[];
              if(signPhoto.length>0){
                signPhoto.forEach((item)=>{
                    if(item!= ""){
                      signImgList.push(getApp().globalData.nginxIp+"/"+item)
                    }
                })
                obj['signImgList']=signImgList
              }
            }
            
            this.setData(obj) 
          }
      })
    }
    
  },

  onInput(e) {
    let name = e.currentTarget.dataset.name
    let value = e.detail.value
    this.setData({
      [`orderInfo.${name}`]: value
    })
    
  },

  printClick() {
    let order = getApp().globalData.pageData[constants.page_orderinfo]
    if (order) {
      getApp().globalData.pageData[constants.page_print] = order
      wx.navigateTo({
        url: '/pages/print/print',
      })
    }
    
  },

  maxViewImg(e){
    let imgUrl=e.currentTarget.dataset.img
    wx.previewImage({
        current:imgUrl,
        urls: [imgUrl],
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})